package org.dev.biz_search.views

import android.content.Context
import android.util.AttributeSet
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import org.dev.hi.hiui.item.HiAdapter
import org.dev.hi.hiui.recyclerview.HiRecyclerView
import org.dev.pub.items.GoodsItem
import org.dev.pub.model.GoodsModel

/**
 * Author WenPing
 * CreateTime 2021/8/30.
 * Description:商品的搜索
 */
class GoodsSearchView
@JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null,
    defStyle: Int = 0
) : HiRecyclerView(context, attributeSet, defStyle) {

    init {
        layoutManager = LinearLayoutManager(
            context, LinearLayoutManager.VERTICAL, false
        )
        adapter = HiAdapter(context)
        layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
    }

    fun bindData(
        list: List<GoodsModel>,
        loadInit: Boolean
    ) {
        val dataItems = mutableListOf<GoodsItem>()
        // 遍历商品的数据,转换成集合
        for (item in list) {
            dataItems.add(GoodsItem(item, true))
        }
        val hiAdapter = adapter as HiAdapter
        if (loadInit) hiAdapter.clearItems()
        // 添加数据并刷新
        hiAdapter.addItems(dataItems, true)

    }


}